﻿/*  
    Copyright (C) <2007-2013>  <Kay Diefenthal>

    ArgusTv.UI.MediaCenter is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ArgusTv.UI.MediaCenter is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ArgusTv.UI.MediaCenter.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using ArgusTV.UI.MediaCenter.Logging;
using ArgusTV.UI.MediaCenter.Properties;
using ArgusTV.UI.MediaCenter.Views;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;

namespace ArgusTV.UI.MediaCenter.ViewModels
{
    public class SettingsModel : ModelItem
    {
        #region Private Fields

        private readonly AddInHost _host;
        private readonly HistoryOrientedPageSession _session;

        #endregion

        #region Constructor

        /// <summary>
        /// SettingsModel Initialize a new Instance of <see cref="SettingsModel"/> Class.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="host"></param>
        public SettingsModel(HistoryOrientedPageSession session, AddInHost host)
        {
            _session = session;
            _host = host;
        }

        #endregion

        #region Properties

        /// <summary>
        /// MediaCenterEnvironment
        /// </summary>
        public MediaCenterEnvironment MediaCenterEnvironment
        {
            get
            {
                if (_host == null) return null;
                return _host.MediaCenterEnvironment;
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// CreateSettingsView Initialize a new Instance of <see cref="SettingsView"/> Class.
        /// </summary>
        /// <returns></returns>
        private SettingsView CreateSettingsView()
        {
            try
            {
                var view = new SettingsView();
                CreateViewSettingsSections(view);
                view.IsConnectionSettingsSectionVisible = true;
                view.IsGuideSettingsSectionVisible = false;
                view.IsAboutSettingsSectionVisible = false;
                view.ServerName = new EditableText(view);
                view.ServerIpAddress = new EditableText(view);
                view.ServerMacAddress = new EditableText(view);
                view.ServerPort = new EditableText(view) {Value = "49942"};
                if (string.IsNullOrEmpty(Settings.Default.ServerName))
                {
                    view.ServerName.Value = Utils.GetLocalHostName();
                    view.ServerIpAddress.Value = Utils.GetIpAddress(view.ServerName.Value);
                    view.ServerMacAddress.Value = Utils.GetMacAddress(view.ServerIpAddress.Value);
                }
                else
                {
                    view.ServerName.Value = Settings.Default.ServerName;
                    view.ServerIpAddress.Value = Utils.GetIpAddress(view.ServerName.Value);
                    view.ServerMacAddress.Value = Utils.GetMacAddress(view.ServerIpAddress.Value);
                }
                view.ServerNameChanged += delegate(object sender, EventArgs args)
                    {
                        var settingsView = (SettingsView) sender;
                        settingsView.ServerIpAddress.Value = Utils.GetIpAddress(settingsView.ServerName.Value);
                    };
                view.ServerIpAddressChanged += delegate(object sender, EventArgs args)
                    {
                        var settingsView = (SettingsView) sender;
                        settingsView.ServerMacAddress.Value = Utils.GetMacAddress(settingsView.ServerIpAddress.Value);
                    };
                return view;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "SettingsViewModel", "CreateView", exception));
                return null;
            }
        }

        /// <summary>
        /// CreateViewSettingsSections
        /// </summary>
        /// <param name="view"></param>
        private void CreateViewSettingsSections(SettingsView view)
        {
            try
            {
                var list = new ArrayListDataSet(view);
                var one = new ModelItem(view, Resources.SettingsSectionConnectionDescription);
                list.Add(one);
                list.Add(new ModelItem(view, Resources.SettingsSectionGuideDescription));
                list.Add(new ModelItem(view, Resources.SettingsSectionAboutDescription));
                var filters = new Choice(view, null, list) {Chosen = one};
                view.SettingsSectionChanged += delegate(object sender, EventArgs args)
                    {
                        var settingsView = (SettingsView) sender;
                        var index = settingsView.SettingsSections.ChosenIndex;
                        switch (index)
                        {
                            case 0:
                                settingsView.IsConnectionSettingsSectionVisible = true;
                                settingsView.IsGuideSettingsSectionVisible = false;
                                settingsView.IsAboutSettingsSectionVisible = false;
                                break;
                            case 1:
                                settingsView.IsConnectionSettingsSectionVisible = false;
                                settingsView.IsGuideSettingsSectionVisible = true;
                                settingsView.IsAboutSettingsSectionVisible = false;
                                break;
                            case 2:
                                settingsView.IsConnectionSettingsSectionVisible = false;
                                settingsView.IsGuideSettingsSectionVisible = false;
                                settingsView.IsAboutSettingsSectionVisible = true;
                                break;
                        }
                    };
                view.SettingsSections = filters;
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "SettingsViewModel", "CreateViewSettingsSections", exception));
            }

        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Navigate to SettingsView
        /// </summary>
        public void GoToSettingsView()
        {
            try
            {
                var uiProperties = new Dictionary<string, object>();
                uiProperties["SettingsView"] = CreateSettingsView();
                _session.GoToPage("resx://ArgusTV.UI.MediaCenter/ArgusTV.UI.MediaCenter.Resources/SettingsView",
                                  uiProperties);
            }
            catch (Exception exception)
            {
                Logger.Error(string.Format("{0}{1}:{2}", "SettingsViewModel", "GoToSettingsView", exception));
            }

        }

        #endregion
    }
}